from flask import Blueprint, render_template, request, flash, redirect, url_for, session
from flask_mail import Mail, Message
import random, time

verification = Blueprint('verification', __name__)

# otp_dict = {}

# 发送验证码的路由
@verification.route('/send_verification_code', methods=['GET', 'POST'])
def send_verification_code():
    if request.method == 'POST':
        email = request.form.get('email')
        otp = str(random.randint(100000, 999999))
        session[email] = {'otp': otp, 'time': time.time()}  # 使用 session 存储验证码
        # 发送邮件
        msg = Message('GlossAI | Register', recipients=[email])
        msg.body = f'[GlossAI] Your verification code is: {otp}, it is valid for 5 minutes.'
        mail = Mail()
        mail.send(msg)

        flash('Verification code sent to your email!', 'success')
        return '', 204  # 204 No Content, 代表请求成功，但无内容返回


